#include <Servo.h> 
Servo myservo; 
#include "U8glib.h"/*I2C协议*/
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); 
const uint8_t bitmap_z [] U8G_PROGMEM  ={  
0x00,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,
0x21,0x08,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x41,0x08,0x41,0x28,0x80,0x10,
}; //用字

const uint8_t bitmap_w [] U8G_PROGMEM  ={  
0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,
0x21,0x08,0x21,0x08,0x21,0x08,0x3F,0xF8,0x21,0x0A,0x01,0x02,0x01,0x02,0x00,0xFE,
}; //电字


int pos = 0; // 用于存储舵机位置的变量
int jidianqi = 12;
int duoji = 9;
int is_quick = 11;
int readvoltagepin = A6;
int readcurrentpin = A0;
int is_cut_off = 0;
int speakerpin= 4;
int Hz = 750; 
int speakertime = 1000;
int a=0;
double electronic = 0.0;
double p = 0.0;
double liminal = 0.40;

void panduan_quick()
{
  if(digitalRead(is_quick))
  {
    liminal = 0.85
      ;
  }
  else
  {
    liminal = 0.40;
  }
}
void draw(void) {
  u8g.setColorIndex(1);
  u8g.drawBitmapP ( 0 , 45 , 2 , 16 , bitmap_z); 
  u8g.drawBitmapP ( 17 , 45 , 2 , 16 , bitmap_w ); 
  u8g.drawStr(17+16,47,":");
  u8g.setPrintPos(17+16+10,47);
  u8g.print(electronic);
  u8g.setPrintPos(17+16+10+40,47);
  u8g.print("/1000");
 }//用电量
 
void setup() {
  myservo.attach(duoji);
  pinMode(jidianqi,OUTPUT);
  pinMode(readcurrentpin,INPUT);
  pinMode(readvoltagepin,INPUT);
  pinMode(speakerpin,OUTPUT);
  pinMode(is_quick,INPUT);
  {//oled设置
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) 
    u8g.setColorIndex(255);     // white
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(1);         // max intensity 最大强度，原先是3
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);         // pixel on
  //设置不同的字体
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_6x10);
  u8g.setFontRefHeightExtendedText();
  u8g.setDefaultForegroundColor();
  u8g.setFontPosTop();//使用drawStr显示字符串时，默认标准为显示字符的左下角坐标。
  //本函数的功能可理解为将坐标位置改为显示字符串的左上角为坐标标准
  }
  tone(speakerpin, Hz, speakertime);
}

void loop() {
  digitalWrite(jidianqi,HIGH);
  int readingcurrent = analogRead(readcurrentpin);
  int readingvoltage = analogRead(readvoltagepin);
  double realcurrent = ((double)readingcurrent / 1023.0 * 5 - 2.50) / 0.185;
  double realvoltage = (double)readingvoltage / 1023.0 * 5 * 5.0;
  p = realcurrent * realvoltage;
  electronic += p / 3600.0;
  panduan_quick();
  if(realcurrent < liminal)
  {
    for(a=0;a < 10; a++)
    {
      int readingcurrenttemp = analogRead(readcurrentpin);
      double tempcurrent = ((double)readingcurrenttemp / 1023.0 * 5 - 2.50) / 0.185;
      u8g.firstPage();
  do {
  u8g.drawStr(0,0,"V:");
  u8g.drawStr(0,15,"A:");
  u8g.drawStr(0,30,"P:");
  u8g.setPrintPos(30,0);
  u8g.print(realvoltage);
  u8g.setPrintPos(30,15);
  u8g.print(tempcurrent);
  u8g.setPrintPos(30,30);
  u8g.print(p);
  draw();
  }while( u8g.nextPage() );
  panduan_quick();
      if(tempcurrent >= liminal)
      {
        is_cut_off = 1;
        break;
      }
      delay(100);
    }
    if(is_cut_off == 0)
    {
      tone(speakerpin, Hz, speakertime);
      //蜂鸣器发声
      for(pos = 90; pos<=270; pos+=1) // 从 180 度-0 度 
      { 
         myservo.write(pos); // 输入对应的角度值，舵机会转到此位置 
         delay(15); // 15ms 后进入下一个位置
      } 
    }
  }
  
  u8g.firstPage();
  do {
  u8g.drawStr(0,0,"V:");
  u8g.drawStr(0,15,"A:");
  u8g.drawStr(0,30,"P:");
  u8g.setPrintPos(30,0);
  u8g.print(realvoltage);
  u8g.setPrintPos(30,15);
  u8g.print(realcurrent);
  u8g.setPrintPos(30,30);
  u8g.print(p);
  draw();
  }while( u8g.nextPage() );
  delay(1000);
  is_cut_off = 0;
}
